<template>
  <teamListCard :team-list="teamList" v-if="teamList.length>0&&teamList!==null">
    <template v-slot="slotProps">
      <van-button
        size="small"
        plain
        type="default"
        @click="dissoleTeam(slotProps.id)"
        style="margin-right: 10px;"
        >解散</van-button
      >
      <van-button
        size="small"
        
        type="danger"
        @click="exitTeam(slotProps.id)"
        style="margin-right: 10px;"
        >退出</van-button
      >
      <van-button size="small" type="warning" @click="updateTeam(slotProps.id)"
        >编辑</van-button
      >
    </template>
  </teamListCard>
  <van-empty image="search" description="还没有创建队伍" v-else />
  <van-notify id="van-notify" />
</template>
<script setup>
import useStore from '../store'
import { ref, onMounted, onUnmounted } from 'vue'
import teamListCard from '../components/teamListCard.vue'
import { hasCreatedTeam, quitTeam, deleteTeam } from '../api/team/team'
import { onShow } from '@dcloudio/uni-app';



const teamList = ref([])
const getCreateTeamList = async () => {
  const res = await hasCreatedTeam()
  console.log(res)
  teamList.value = res.data.teamUserVOList
}
//解散队伍
const dissoleTeam = (teamId) => {
  console.log(teamId)
  uni.showModal({
    title: '提示',
    content: '确认解散队伍？解散后不可恢复！',
    success: async (res) => {
      if (res.confirm) {
        console.log('用户点击确定')
        const res = await deleteTeam(teamId)
        if (res.code === 0) {
          uni.showToast({
            title: '解散成功',
            duration: 2000,
            icon: 'success',
            success:()=>{
              getCreateTeamList()
            }
          })
        } else {
          uni.showToast({
            title: res.description,
            duration: 2000,
            icon: 'fail',
          })
        }
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    },
  })
}
onShow(()=>{
  getCreateTeamList()
})
//退出队伍
const exitTeam = (teamId) => {
  uni.showModal({
    title: '提示',
    content: '确认退出队伍？',
    success: async (res) => {
      if (res.confirm) {
        console.log('用户点击确定')
        const res = await quitTeam({teamId})
        
        if (res.code === 0) {
          uni.showToast({
            title: '成功',
            duration: 2000,
            icon: 'success',
            success:()=>{
              getCreateTeamList()
            }
          })
        } else {
          uni.showToast({
            title: res.description,
            duration: 2000,
            icon: 'fail',
          })
        }
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    },
  })
}
//编辑队伍
const updateTeam = (id) => {
  useStore().team().getTeamUpdateID(id)
  uni.navigateTo({
    url:'/pageTeam/updateTeam'
  })

}
    useStore().setting().btnStatusChange(1)
onMounted(() => {
  // getCreateTeamList()
})
onUnmounted(() => {
  useStore().setting().btnStatusChange(0)
})
</script>
